package com.sky.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/20 22:52
 */
@Api(tags = "菜品相关接口")
@RestController
@RequestMapping(value = "/admin/dish")
@Slf4j
public class DishController {
    @Resource
    DishService dishService;

    /**
     * 新增菜品和口味
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增菜品和口味")
    public Result saveDishAndFlavor(@RequestBody DishDTO dishDTO) {
        dishService.saveDishAndFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping(value = "/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> dishPageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("dishPageQueryDTO = {}",dishPageQueryDTO);
        // 开始分页
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        // 以下sql查询后会自动完成分页
        List<DishVO> dishes = dishService.pageQuery(dishPageQueryDTO);
        PageInfo<DishVO> dishVOPageInfo = new PageInfo<>(dishes);
        PageResult pageResult = new PageResult(dishVOPageInfo.getTotal(), dishVOPageInfo.getList());
        log.info("pageResult = {}",pageResult);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "批量删除菜品")
    // @RequestParam(value = "ids") List<Long> ids 该注解可以将前端传来的字符串ids转换为集合ids
    public Result batchDeleteDish(@RequestParam(value = "ids") List<Long> ids){
        log.info("ids = {}",ids);
        dishService.batchDelete(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品数据
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询菜品数据")
    public Result<DishVO> getById(@PathVariable(value = "id") Long id){
        DishVO dishVO = dishService.getById(id);
        dishVO.setId(id);
        log.info("dishVO = {}",dishVO);
        return Result.success(dishVO);
    }

    /**
     * 更新菜品和口味信息
     * @return
     */
    @ApiOperation(value = "更新菜品和口味信息")
    @PutMapping
    public Result updateDishWithFlavor(@RequestBody DishDTO dishDTO){
        dishService.updateDishWithFlavor(dishDTO);
        return Result.success();
    }

    @ApiOperation(value = "修改菜品状态")
    @PostMapping(value = "/status/{status}")
    public Result setDishStatus(@PathVariable(value = "status") Integer status,Long id){
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishService.setDishStatus(dish);
        return Result.success();
    }

    /**
     * 根据分类id查找菜品
     * @param categoryId
     * @return
     */
    @ApiOperation(value = "根据分类id查找菜品")
    @GetMapping(value = "/list")
    public Result<List<Dish>> queryByCategoryId(Long categoryId){
        log.info("categoryId = {}",categoryId);
        List<Dish> dishes = dishService.getByCategoryId(categoryId);
        return Result.success(dishes);
    }
}
